package com.wjw.java.basic.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 按照Book的价格降序排序
 */
public class ArraysExercise {
    public static void main(String[] args) {
        Book [] books = new Book[5];
        books[0] = new Book("Java",99.99);
        books[1] = new Book("HTML",120.88);
        books[2] = new Book("CSS",79.99);
        books[3] = new Book("Mysql",88.88);
        books[4] = new Book("Oracle",188.88);
        //自定义排序
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;
                double result = book2.getPrice() - book1.getPrice();
                if(result > 0){
                    return 1;
                }else if(result < 0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        System.out.println(Arrays.toString(books));
    }
}
//书籍类
class Book{
    String name;
    double price;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book() {
    }

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}